<?php

include_once dirname(__DIR__).'/third_party/phpcode_shield.php';		//加载防注入代码
class Y extends CI_Controller
{
	/*
		构造函数
	*/
	function __construct()
	{
		header('Content-Type: text/html; charset=utf-8');
		parent::__construct();
		$this->load->helper('url');
	}

	/*
		判断是否微信的
	*/
	public function is_weixin()
	{
		if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
			return true;
		}
		return false;
	}

	/*
		提示在微信浏览器打开
	*/
	public function warmingWX()
	{
		$data['base_url'] = base_url();
		$this->load->view("warming_wx",$data);
	}


	    public function all($state='', $scope='snsapi_userinfo'){
       
        $baseurl = base_url();
        $arr = explode(':', $baseurl, 2);
        $base_host = substr($arr[1], 2, -1);
        $arr = explode("_", $state, 2);
        if($base_host != $_SERVER['HTTP_HOST']){
            $direct_url = base_url('f/'.$arr[0]);
            Header("Location:".$direct_url);
            exit();
        }

        if ($_SESSION['WxOpenID']) {
            if ($arr[0] == 'ym' || $arr[0] == 'yh') {    //大厅或个人主页
                $dest_path = "f/" . $arr[0];
            } else {
                if (isset($arr[1]) && $arr[1]) {
                    if ($arr[0] == 're') {         //包包
                        $dest_path = "ay/rpD?red_code=" . $arr[1];
                    } else if ($arr[0] == 'invite') {  //管理-邀请函
                        $dest_path = "manage/invite?code=" . $arr[1];
                    } else {      //游戏房间
                        $dest_path = "f/" . $arr[0] . '?i=' . $arr[1];
                    }
                }
            }
            $data['dest_url'] = $this->config->item("base_url") . $dest_path;
        }else{
            $data['dest_url'] = $this->config->item("callback_url") . 'y/all2/' . $state . '/' . $scope;
        }
        $data['image_url']= Game_Const::ImageUrl;
        $this->load->view("ks.php",$data);
    }

	/*
		总入口
	*/
	public function all_bak($state='', $scope='snsapi_userinfo')	{
//	    if ($_SERVER['HTTP_HOST'] != $this->config->item('agreement_url')){
//            $direct_url = 'http://'.$this->config->item('agreement_url').$_SERVER['REQUEST_URI'];
//            Header("Location:".$direct_url);
//        }
		$state = filter_var($state);
		if($state)
		{
			$data['image_url']= Game_Const::ImageUrl;
			
                $data['dest_url'] = $this->config->item("callback_url") . 'y/all2/' . $state . '/' . $scope;
           
			$this->load->view("ks.php",$data);
		}
		else
		{
			show_404();exit;
		}
	}

	public function all2($state='', $scope='snsapi_userinfo')
	{
		$redirect_uri =  $this->config->item("callback_url") . 'y/IDall' ;
		$direct_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.Game_CONST::WX_Appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';

		Header("Location:".$direct_url);
	}

	public function IDall()
	{
		if(isset($_GET['code']) )
		{
			$code = $_GET['code'];
			$state = $_GET['state'];

			$arr = explode("_", $state, 2);

			if($arr[0]=='ym' || $arr[0]=='yh'){    //大厅或个人主页
				$dest_path = "f/".$arr[0];
			} else {
				if(isset($arr[1]) && $arr[1]){
					if($arr[0]=='re'){         //包包
						$dest_path = "ay/rpD?red_code=".$arr[1];
					} else if($arr[0]=='invite') {  //管理-邀请函
						$dest_path = "manage/invite?code=".$arr[1];
					} else {      //游戏房间
						$dest_path = "f/".$arr[0].'?i='.$arr[1];
					}
				} else {
					show_404(__FUNCTION__.":error state");exit();
				}
			}

			$r = urlencode(base64_encode($code.' '.$dest_path));

			$direct_url = base_url('f/route?r='.$r);
			Header("Location:".$direct_url);exit();

		} else {
			show_404(__FUNCTION__.":lack code");exit();
		}
	}

	public function ym($scope='snsapi_userinfo')
	{
		$this->all('ym_', $scope);
	}

	public function yh($scope='snsapi_userinfo')
	{
		$this->all('yh_', $scope);
	}

	/*
    9人三公房间跳转
    */
	public function sg9($state='', $scope='snsapi_userinfo')
	{
		$this->all('sg9_'.$state, $scope);
	}


	/*
    二八杠房间跳转
    */
	public function eba($state='', $scope='snsapi_userinfo')
	{
		$this->all('eba_'.$state, $scope);
	}



	/*
    三公房间跳转
    */
	public function sg($state='', $scope='snsapi_userinfo')
	{
		$this->all('sg_'.$state, $scope);
	}



	/*
        斗牛房间跳转 12人
    */
	public function mb($state='', $scope='snsapi_userinfo')
	{
		$this->all('mb_'.$state, $scope);
	}



	/*
		斗牛房间跳转 9人6倍
	*/
	public function b6b($state='', $scope='snsapi_userinfo')
	{
		$this->all('b6b_'.$state, $scope);
	}

	/*
		斗牛房间跳转 9人8倍
	*/
	public function b8b($state='', $scope='snsapi_userinfo')
	{
		$this->all('b8b_'.$state, $scope);
	}

	/*
		斗牛房间跳转 9人
	*/
	public function nb($state='', $scope='snsapi_userinfo')
	{
		$this->all('nb_'.$state, $scope);
	}


	/*
		斗牛房间跳转 6人
	*/
	public function b($state='', $scope='snsapi_userinfo')
	{
		$this->all('b_'.$state, $scope);
	}


	/*
		麻将房间跳转
	*/
	public function ma($state='', $scope='snsapi_userinfo')
	{
		$this->all('ma_'.$state, $scope);
	}


	/*
		斗地主房间跳转
	*/
	public function l($state='', $scope='snsapi_userinfo')
	{
		$this->all('l_'.$state, $scope);
	}


	/*
		6人炸金花房间跳转
	*/
	public function yf($state='', $scope='snsapi_userinfo')
	{
		$this->all('yf_'.$state, $scope);
	}

	/*
		9人炸金花房间跳转
	*/
	public function z9($state='', $scope='snsapi_userinfo')
	{
		$this->all('z9_'.$state, $scope);
	}
    /*
            9人炸金花房间跳转
        */
    public function z12($state='', $scope='snsapi_userinfo')
    {
        $this->all('z12_'.$state, $scope);
    }
    /*
    *牌九房间跳转
    */
    public function pj($state='', $scope='snsapi_userinfo')
    {
            $this->all('pj_'.$state, $scope);
	}
	
	/*
	*大吃小房间跳转
	*/
	public function dcx($state='', $scope='snsapi_userinfo')
	{
		$this->all('dcx_'.$state, $scope);
	}
		
    /*
    *鱼虾蟹房间跳转
    */
    public function yxx($state='', $scope='snsapi_userinfo')
    {
        $this->all('yxx_'.$state, $scope);
    }


    /*
    *赖子牛牛房间跳转
    */
    public function laib($state='', $scope='snsapi_userinfo')
    {
		$this->all('laib_'.$state, $scope);
    }

	/*
	*明牌炸金花房间跳转
	*/
	public function mpZjh($state='', $scope='snsapi_userinfo')
	{
		$this->all('mpZjh_'.$state, $scope);
	}

	/*
		血拼炸金花房间跳转
	*/
	public function xpjh($state='', $scope='snsapi_userinfo')
	{
		$this->all('xpjh_'.$state, $scope);
	}


	//红包
	public function re($state, $scope='snsapi_userinfo')
	{
		$this->all('re_'.$state, $scope);
	}


	// 管理功能
	public function invite($state, $scope='snsapi_userinfo')
	{
		$this->all('invite_'.$state, $scope);
	}

}
